﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

public partial class MRP_DailyReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_CmdFieldFire(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);
        DateTime dt=DateTime.Today;
        
        if (e.CommandText.Equals("录入生产单"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            string sqlCommand = "MRP_CheckAndAdd_DailyReport";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "ProduceID", DbType.Int32, id);

            if (!string.IsNullOrEmpty(e.Arg))
            {
                if (!DateTime.TryParse(e.Arg, out dt))
                    dt = DateTime.Today;
            }
            db.AddInParameter(dbCommand, "CheckDate", DbType.Date, dt);
            db.ExecuteNonQuery(dbCommand);

            Session["dbWhereDetailsInSession"] = string.Format("T.ProduceDate='{0:d}'", dt);


            Response.Redirect(string.Format("MRP_DailyReport.aspx?ID={0}&MODE=edit",id));
            
        }
        else if (e.CommandText.Equals(""))
        {
        }
    }
    protected void UIServer1_DBChange(object sender, Evolutility.UIServer.DatabaseEventArgs e)
    {
        if (e.Action == Evolutility.UIServer.DBAction.UPDATE)
        {

            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            int id = e.ID;

            string sqlCommand = "MRP_ChangeDailyReportStatus";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.AddInParameter(dbCommand, "itemID", DbType.Int32, id);
            //db.AddInParameter(dbCommand, "@ErrorRange", DbType.Decimal,1);//假设误差为1%
            int iret=db.ExecuteNonQuery(dbCommand);

        }
    }
}
